#!/usr/bin/python 
#vim: set fileencoding:utf-8
import requests
from bs4 import BeautifulSoup
import MySQLdb
import threading 
import time

class myThread (threading.Thread):
 def __init__(self,name,i):
  threading.Thread.__init__(self)
  self.i = i
  self.name = name
 def run(self):
  print("Starting "+self.name)
  stars = get_star(self.name,self.i)
  names = get_name(self.name,self.i)
  for i in range(0,len(names)):
   print(str(i)+" "+names[i]+":"+stars[i])
  print("Exiting "+self.name)

def get_star(name,i):
  headers = {'user_agent':'Mozilla/5.0 (Windows NT 6.1;Win64;x64) AppletWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Host':'movie.douban.com'}
  movie_list = []
  link = 'https://movie.douban.com/top250?start='+str(i*25)
  r = requests.get(link,headers=headers,timeout=10)
  print(str(i+1),"status_code",r.status_code)
  soup = BeautifulSoup(r.text,'html.parser')
  div_list = soup.find_all('div',class_='star')
  for each in div_list:
    movie = each.find_all('span')
    movie_list.append(movie[1].text.strip())
  return movie_list

def get_name(name,i):
 headers = {'user_agent':'Mozilla/5.0 (Windows NT 6.1;Win64;x64) AppletWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Host':'movie.douban.com'}
 movie_name = []
 link = 'https://movie.douban.com/top250?start='+str(i*25)
 r = requests.get(link,headers=headers,timeout=10)
   #print(str(i+1),"status_code",r.status_code)
 soup = BeautifulSoup(r.text,'html.parser')
 div_name = soup.find_all('div',class_='hd')
 for each in div_name:
  name_m = each.a.span.text.strip()
  movie_name.append(name_m)
 return movie_name
threads = []
start = time.time()
for i in range(0,10):
 thread = myThread("Thread-"+str(i+1),i)
 thread.start()
 threads.append(thread)
for t in threads:
 t.join()

end = time.time()

print("Exiting Main Thread",end-start)
